/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : TimeUtil.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Jan 16, 2009  Raghu Sarraff			       Created
 */
package net.lwf.handler.time.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimeUtil {
	
	/**
	 * Gets the time difference. 
	 * 
	 * If the difference is less than zero then the second time occurs before the 
	 * first time attribute (OR first attribute occurs after the second).
	 * 
	 * If the difference is greater than zero then the second time occurs after the 
	 * first time attribute (OR first attribute occurs before the second).
	 *  
	 * @param time1
	 * @param time2
	 * @return
	 */
	/*public static long getElapsedTime(Time time1, Time time2) {
		time1.print();
		time2.print();
		long time1millis = time1.toCalendar().getTimeInMillis();
		long time2millis = time2.toCalendar().getTimeInMillis();
		 return (time2millis - time2millis)/1000;
	}*/
	/*
	public static long getElapsedTime(Time time1, Time time2) {
		time1.print();
		time2.print();
		long time1millis = toSeconds(time1);
		long time2millis = toSeconds(time2);
		 return time2millis - time1millis;
	}
	*/
/*	public static long getElapsedTime(Time time1, Time time2) {
		time1.print();
		time2.print();
		Date date = new Date();
		
		Calendar cal = Calendar.getInstance();
		
		GregorianCalendar cal1 = new GregorianCalendar();
		System.out.println("-------- Current time: Year: " + cal.get(Calendar.YEAR) + ", Month: " + cal.get(Calendar.MONTH) + ", Date: " + cal.get(Calendar.DATE));
		cal1.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), (int)time1.getHours(), (int)time1.getMinutes(), (int)time1.getSeconds());
		GregorianCalendar cal2 = new GregorianCalendar();
		cal2.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), (int)time2.getHours(), (int)time2.getMinutes(), (int)time2.getSeconds());		
		
		long time1millis = cal1.getTime().getTime();
		System.out.println("...... Time millis: " + cal1.getTimeInMillis() + ",  test2: " + time1millis);
		long time2millis = cal2.getTime().getTime();
		
		return (time2millis - time1millis)/1000;
	}*/
	
	public static long getElapsedTime(Time time1, Time time2) {
		Calendar cal = Calendar.getInstance();
		GregorianCalendar cal1 = new GregorianCalendar();
		cal1.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), (int)time1.getHours(), (int)time1.getMinutes(), (int)time1.getSeconds());
		GregorianCalendar cal2 = new GregorianCalendar();
		cal2.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), (int)time2.getHours(), (int)time2.getMinutes(), (int)time2.getSeconds());		
		
		long time1millis = cal1.getTime().getTime();
		long time2millis = cal2.getTime().getTime();
		
		return (time2millis - time1millis)/1000;
	}	
    
	public static String getTimeFormat(long hours, long minutes, long seconds) {
//		String format = "";
		
//		if(hours != 0) {
//			format += "hh:mm";
//		} else {
//			if(minutes != 0) {
//				format += "mm";
//			}
//		}
//		if(seconds != 0) {
//			format += ":ss";
//		}
//		
//		if(hours != 0) {
//			format += " a";
//		}
		
		String format = "hh:mm";
		
		if(seconds != 0) {
			format += ":ss";
		}
		

		format += " a";

		return format;
	} //getTimeFormat
	
	public static String getFormattedTime(long hours, long minutes, long seconds) {
		DateFormat df = new SimpleDateFormat(getTimeFormat(hours, minutes, seconds));
    	Calendar cal = Calendar.getInstance();
    	cal.set(Calendar.YEAR, Calendar.MONTH, Calendar.DATE, (int)hours, (int)minutes, (int)seconds);
    	Date date = cal.getTime();
    	try {
    		return df.format(date);
    	}
    	catch(Exception ex) {
    		ex.printStackTrace();
    	}
    	return "";
		
	}
	
	//----------------------------------------------------------------------------------------------
	// Conversion methods
	//----------------------------------------------------------------------------------------------
	/**
	 * Converts all attributes into hours. 
	 * Available hours are rounded to the nearest day,
	 * minutes are rounded to the nearest hour and 
	 * seconds are rounded to the nearest minute.
	 * 
	 * @return
	 */
	public static long toYears(Time time) {
		long yearcounter = 0;
		long remainingdays = toDays(time);
		while(remainingdays - 365 >= 0) {
			yearcounter++;
			remainingdays = remainingdays - 365;
		}
		if(remainingdays >= 200) { //????
			yearcounter++;
		}
        return yearcounter;
	}
	
	/**
	 * Converts all attributes into hours. 
	 * Available hours are rounded to the nearest day,
	 * minutes are rounded to the nearest hour and 
	 * seconds are rounded to the nearest minute.
	 * 
	 * @return
	 */
	public static long toDays(Time time) {
		long daycounter = 0;
		long remaininghrs = toHours(time);
		while(remaininghrs - 24 >= 0) {
			daycounter++;
			remaininghrs = remaininghrs - 24;
		}
		if(remaininghrs >= 12) {
			daycounter++;
		}
        return daycounter;
	}
	
	/**
	 * Converts all attributes into hours. 
	 * Available minutes are rounded to the nearest hour and 
	 * seconds are rounded to the nearest minute.
	 * 
	 * @return
	 */
	public static long toHours(Time time) {
		long hourcounter = 0;
		long remainingmins = toMinutes(time);
		while(remainingmins - 60 >= 0) {
			hourcounter++;
			remainingmins = remainingmins - 60;
		}
		if(remainingmins >= 30) {
			hourcounter++;
		}
        return hourcounter;
	}
		
	/**
	 * Converts all attributes into minutes. 
	 * Available seconds are rounded to the nearest minute.
	 * 
	 * @return
	 */
	public static long toMinutes(Time time) {
		long mincounter = 0;
		long remainingsecs = toSeconds(time);
		while(remainingsecs - 60 >= 0) {
			mincounter++;
			remainingsecs = remainingsecs - 60;
		}
		if(remainingsecs > 0) {
			mincounter++;
		}
        return mincounter;
	}
	
	/**
	 * Converts all attributes into seconds.
	 * 
	 * @return
	 */
	public static long toSeconds(Time time) {
		long converted = time.getSeconds();
		if(time.getMinutes() != 0) {
			converted += time.getMinutes() * 60;
		}
		if(time.getHours() != 0) {
			converted += time.getHours() * 60 * 60;
		}
		if(time.getDays() != 0) {
			converted += time.getDays() * 24 * 60 * 60;
		}
		return converted;
	}
	
	

	public static void main(String[] args) {
/*		Time time = new Time(25,30,0);
		System.out.println("Re-organized time: ");
		time.print();
		time = new Time(5,300,76);
		System.out.println("Re-organized time: ");
		time.print();
		time = new Time(0,30,600);
		System.out.println("Re-organized time: ");
		time.print();
		time = new Time(368, 0,30,0);
		System.out.println("Re-organized time: ");
		time.print();
		time = new Time(30000,30,94);
		System.out.println("Re-organized time: ");
		time.print();
		//Addition
		time = new Time(12,30,0);
		//System.out.println("Initial time: " + TimeUtil.getTimeString(time));
		time.add(10,0,0);
		System.out.println("Re-organized time: ");
		//System.out.println("Final String: " + TimeUtil.getTimeString(time));
		time.print();
*/
		int minutes = 15;
		Time time = new Time(23,45,0);
		for(int i=0; i<60/minutes; i++) {
			System.out.println("Formatted time: " + time.getFormattedTime());
			time.add(0,minutes,0);
		}
	}

    
}

